package cn.edu.gcu.giftems.action;

import cn.edu.gcu.giftems.pojos.Gift;
import cn.edu.gcu.giftems.pojos.Type;
import cn.edu.gcu.giftems.service.GiftService;
import cn.edu.gcu.giftems.service.TypeService;
import cn.edu.gcu.giftems.utils.PageUtils;
import cn.edu.gcu.giftems.utils.export.GiftExportUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * TODO: Description here...
 *
 * @author ZhengHQ
 * @create 2018-09-25
 * @see
 */
public class GiftAction extends ActionSupport implements ModelDriven<Gift> {

    private GiftService giftService =
            (GiftService) new ClassPathXmlApplicationContext("applicationContext.xml")
                    .getBean("giftService");
    private TypeService typeService =
            (TypeService) new ClassPathXmlApplicationContext("applicationContext.xml")
                    .getBean("typeService");
    private Gift gift = new Gift();
    private List<Type> list;

    @Override
    public Gift getModel() {
        return gift;
    }

    public void setGift(Gift gift) {
        this.gift = gift;
    }

    public void setGiftService(GiftService giftService) {
        this.giftService = giftService;
    }

    public void setTypeService(TypeService typeService) {
        this.typeService = typeService;
    }

    public List<Type> getList() {
        return list;
    }

    public void setList(List<Type> list) {
        this.list = list;
    }

    /**
     * 分页查询礼品的执行方法
     */
    private Integer currPage = 1;
    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
    }
    public String findByPage(){
        PageUtils<Gift> pageUtils = giftService.findByPage(currPage);
        //ActionContext.getContext().getValueStack().push(pageUtils);
        //pageUtils.setCurrPage(currPage);
        ActionContext.getContext().put("pageUtils", pageUtils);
        list = typeService.findAll();
        //ActionContext.getContext().put("list", list);
        return "success";
    }

    public String save(){
        giftService.save(gift);
        return this.findByPage();
    }

    /**
     * 更新礼品信息的执行方法
     */
    public String update(){
        if (giftService.findById(gift.getId()) != null)
            giftService.update(gift);
        return this.findByPage();
    }
    /**
     * 根据礼品id删除礼品
     */
    public String delete(){
        gift = giftService.findById(gift.getId());
        if (gift != null)
            giftService.delete(gift);
        return this.findByPage();
    }

    public String edit(){
        if (gift.getId() > 0) {
            Gift.Status raleaseStatus = gift.getStatus();
            gift = giftService.findById(gift.getId());
            gift.setStatus(raleaseStatus);
            giftService.update(gift);
        }
        return this.findByPage();
    }

    public void export()
    {
        GiftExportUtils Export=new GiftExportUtils();
        try {
            Export.export(giftService);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("导出数据有误");
        }
    }

    public String findByParameter() {
        String filter_name = ActionContext.getContext().getParameters().get("filter-name").toString();
        String simple_filter_value;
        if (ActionContext.getContext().getParameters().get("simple-filter-value2").toString().equals("")) {
            simple_filter_value = ActionContext.getContext().getParameters().get("simple-filter-value").toString();
        }
        else {
            simple_filter_value = ActionContext.getContext().getParameters().get("simple-filter-value2").toString();
        }
        PageUtils<Gift> pageUtils = giftService.findByParameter(currPage, filter_name,simple_filter_value);
        ActionContext.getContext().put("pageUtils", pageUtils);
        return "success";
    }
}
